import os
from google.appengine.ext.webapp import template
import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import datetime


class index(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'views/index.html')
        self.response.out.write(template.render(path, None))

class post(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'views/post.html')
        self.response.out.write(template.render(path, None))

class Greeting(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)

class MessageBoard(webapp.RequestHandler):
    def get (self):
        greetings_query = Greeting.all().order('-date')
        greetings = greetings_query.fetch(10)
    
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'
        
        template_values = {
            'greetings': greetings,
            'url': url,
            'url_linktext': url_linktext,
        }

        path = os.path.join(os.path.dirname(__file__), 'views/old/message.html')
        self.response.out.write(template.render(path, template_values))

class About(webapp.RequestHandler):
    def get (self):
        path = os.path.join(os.path.dirname(__file__), 'views/old/about.html')
        self.response.out.write(template.render(path, ''))

class Guestbook(webapp.RequestHandler):
    def post (self):
#        self.response.out.write('<html><body>You wrote: <pre>')
#        self.response.out.write(cgi.escape(self.request.get('content')))
#        self.response.out.write('</pre></body></html>')
        greeting = Greeting()
        if users.get_current_user():
            greeting.author = users.get_current_user()
        
        #greeting.content = self.request.get('content')
        content = self.request.get('content')
        if content:
            greeting.content = content
            greeting.put()
            self.redirect('/message')
        else:
            self.redirect('/error')

class PrintEnvironmentHandler(webapp.RequestHandler):
    def get(self):
       # for name in os.environ.keys():
       #     self.response.out.write("%s = %s<br />\n" % (name, os.environ[name]))
       path = os.path.join(os.path.dirname(__file__), 'views/old/environ.html') 
       # print os.environ.keys()
       name = os.environ.keys();
       value = os.environ.values();
       environs = zip(name, value)
       self.response.out.write(template.render(path, { "environs": environs })) 

class Logs(webapp.RequestHandler):
    def get (self):
        #log = Log()
        path = os.path.join(os.path.dirname(__file__), 'views/old/logs.html')
        self.response.out.write(template.render(path, ''))

class Error(webapp.RequestHandler):
    def get (self):
        path = os.path.join(os.path.dirname(__file__), 'views/old/error.html')
        self.response.out.write(template.render(path, ''))


application = webapp.WSGIApplication([('/', index),
                                      ('/post.html', post),
                                      ('/sign', Guestbook),
                                      ('/environ', PrintEnvironmentHandler),
                                      ('/about', About),
                                      ('/message', MessageBoard),
                                      ('/logs', Logs),
                                      ('/error', Error),
                                     ],debug=True)

def main ():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
